package com.wenhai.concurrency.thread;

import java.util.Queue;

/**
 * @ClassName Consumer
 * @Description 消费者
 * @Author 谢文海
 * @Date 2021/4/8 1:00
 * @Version 1.0
 **/
public class Consumer {
    private final Queue<Integer> queue;

    public Consumer(Queue<Integer> queue) {
        this.queue = queue;
    }

    public void consumer() {
        synchronized (queue) {
            while (queue.isEmpty()) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "消费者消费: " + queue.poll());
            queue.notifyAll();
        }
    }
}
